<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Commission\User;


use App\Api\Controllers\BaseController;
use App\Services\Count\BillLogCountService;
use App\Services\Count\UserBillCountService;
use App\Services\Count\UserService;
use App\Services\PrintSqlService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CommissionController extends BaseController
{
    use ResponseTrait;

    //按照月份分润

    //用户分润
    public function userCommission(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $userId = $user->user_id;
            //分析当前user的级别
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation($userId);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            //获取分润统计数据
            $where = [
                "curHdIndex" => 0,
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "page" => (int)$request->get("page", 1),
                "pagesize" => (int)$request->get("pagesize", 20),
                "company" => $request->get("company", ""),
                "pay_type" => $request->get("pay_type", 0),
                "store_id" => $request->get("store_id", ""),
                "receive_model" => $request->get("receive_model", 0),
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column
            ];
            $group = ["month","company","pay_type","store_id","receive_model"];
//            if (isset($where["company"])) {
//                array_push($group, "company");
//            }
//            if (isset($where["pay_type"])) {
//                array_push($group, "pay_type");
//            }
//            if (isset($where["store_id"])) {
//                array_push($group, "store_id");
//            }
//            if (isset($where["receive_model"])) {
//                array_push($group, "receive_model");
//            }
            Log::channel("daily")->info("commission", [
                $where,
                $group
            ]);
//            PrintSqlService::listen();
            $userBillCountService = new UserBillCountService();
            $data = $userBillCountService->commissionCount($where, $group);
            return $this->successResponse("ok", $data);
        } catch (\Exception $e) {
            return $this->errorResponse("deug:" . $e->getMessage() . $e->getFile() . $e->getLine());
        }
    }

    //分润明细
    public function userCommissionLog(Request $request): string
    {
        $user = $this->parseToken();
        $userId = $user->user_id;
        //分析当前user的级别
        $userService = new UserService();
        list($idValues, $column) = $userService->handleUserAccountRelation($userId);
        list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
        list($profit_column, $fee_column) = $column;
        //获取分润统计数据
        $where = [
            "curHdIndex" => 0,
            "startDate" => $request->get("startDate"),
            "endDate" => $request->get("endDate"),
            "page" => (int)$request->get("page", 1),
            "pagesize" => (int)$request->get("pagesize", 20),
            "receive_model" => $request->get("receive_model"),
            "store_id" => $request->get("store_id"),
            "company" => $request->get("company", ""),
            "pay_type" => $request->get("pay_type", 0),
            "oem_id" => $oem_id,
            "agent1_id" => $agent1_id,
            "agent2_id" => $agent2_id,
            "agent3_id" => $agent3_id,
            "agent4_id" => $agent4_id,
            "profit_column" => $profit_column,
            "fee_column" => $fee_column,
        ];
        //只能查询当月明细
        if (date("Ym", strtotime($where["startDate"])) != date("Ym", strtotime($where["endDate"]))) {
            return $this->errorResponse("不能跨月查询");
        }
        $billLogCountService = new BillLogCountService();
        $data = $billLogCountService->commissionCountLog($where);
        return $this->successResponse("ok", $data);
    }

    //用户分润
    public function userSubCommission(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $userId = $user->user_id;
            //分析当前user的级别
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation($userId);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            //获取分润统计数据
            $where = [
                "curHdIndex" => 0,
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "page" => (int)$request->get("page", 1),
                "pagesize" => (int)$request->get("pagesize", 20),
                "company" => $request->get("company", ""),
                "pay_type" => $request->get("pay_type", 0),
                "store_id" => $request->get("store_id", ""),
                "receive_model" => $request->get("receive_model"),
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
                "profit_column" => $profit_column,
                "fee_column" => $fee_column
            ];
            $group = ["month"];
            if (!empty($where["company"])) {
                array_push($group, "company");
            }
            if (isset($where["pay_type"])) {
                array_push($group, "pay_type");
            }
            $userBillCountService = new UserBillCountService();
            $data = $userBillCountService->commissionCount($where, $group);
            return $this->successResponse("ok", $data);
        } catch (\Exception $e) {
            return $this->errorResponse("deug:" . $e->getMessage() . $e->getFile() . $e->getLine());
        }
    }

    //分润明细
    public function userSubCommissionLog(Request $request): string
    {
        $user = $this->parseToken();
        $userId = $user->user_id;
        //分析当前user的级别
        $userService = new UserService();
        list($idValues, $column) = $userService->handleUserAccountRelation($userId);
        list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
        list($profit_column, $fee_column) = $column;
        //获取分润统计数据
        $where = [
            ["agent_id", "!=", $userId],
            "curHdIndex" => 0,
            "startDate" => $request->get("startDate"),
            "endDate" => $request->get("endDate"),
            "page" => (int)$request->get("page", 1),
            "pagesize" => (int)$request->get("pagesize", 20),
            "receive_model" => $request->get("receive_model"),
            "oem_id" => $oem_id,
            "agent1_id" => $agent1_id,
            "agent2_id" => $agent2_id,
            "agent3_id" => $agent3_id,
            "agent4_id" => $agent4_id,
            "profit_column" => $profit_column,
            "fee_column" => $fee_column,
            "company" => $request->get("company", ""),
            "pay_type" => $request->get("pay_type", 0),
            "store_id" => $request->get("store_id", ""),
        ];
        //只能查询当月明细
        if (date("Ym", strtotime($where["startDate"])) != date("Ym", strtotime($where["endDate"]))) {
            return $this->errorResponse("不能跨月查询");
        }
        $billLogCountService = new BillLogCountService();
        $data = $billLogCountService->commissionCountLog($where);
        return $this->successResponse("ok", $data);
    }

}